

package com.gc.server.service.impl;
import com.gc.server.dao.SchoolDao;
import com.gc.server.entity.SchoolEntity;
import com.gc.server.entity.UserEntity;
import com.yf.response.RRException;
import org.springframework.transaction.annotation.Transactional;
import com.gc.server.vo.up.SchoolUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.gc.server.vo.down.SchoolDown;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.gc.server.vo.down.SchoolDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.gc.server.dao.SchoolDao;
import com.gc.server.entity.SchoolEntity;
import com.gc.server.vo.SchoolVo;
import com.gc.server.service.SchoolService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Oct 22 19:28:49 CST 2020
 */
@Service
public class SchoolServiceImpl extends BaseServiceImpl<SchoolEntity> implements SchoolService {

    private SchoolDao dao;


    @Autowired
    public void setDao(SchoolDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<SchoolVo> findVo(SchoolVo vo){
        return dao.findVo(vo);
    }

    @Override
    public SchoolVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(SchoolVo vo){
        if(vo.getEntity().getCode()==null){
            dao.save(vo.getEntity());
            vo.getEntity().setCode("C"+vo.getEntity().getId());
        }else {
            vo.getEntity().setId(Long.valueOf(vo.getEntity().getCode().replace("C","")));
        }
        return dao.update(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(SchoolVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<SchoolVo> list) {
        try {
            List<SchoolDown> datas = list.stream().map(item -> new SchoolDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), SchoolDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, SchoolUp.class, new UploadDataListener()).sheet().doRead();
    }

    class UploadDataListener extends AnalysisEventListener<SchoolUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(SchoolUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/

    @Override
    public List<SchoolEntity> findByUserId(Long userId) {
        return dao.findByUserId(userId);
    }

    @Override
    public List<SchoolEntity> findByRouteId(Long routeId) {
        return dao.findByRouteId(routeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId, SchoolEntity schoolEntity) {
        if(schoolEntity.getName()==null||schoolEntity.getName().equals("")){
            throw new RRException("请填写名字");
        }
        //新增
        if(schoolEntity.getId()==null){
            dao.save(schoolEntity);
        }else { //修改
            dao.update(schoolEntity);
            //先删除所有的
            dao.rmUser(userId,schoolEntity.getId());
        }
        //添加药店
        if(schoolEntity.getUserIds()!=null){
            List<Long> userIds = schoolEntity.getUserIds().stream().filter(s -> s!=null).distinct().collect(Collectors.toList());
           if(userIds.size()>0){
               dao.addUser(schoolEntity.getId(),userIds);
           }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId, List<Long> schoolIds) {
        //全部删除
        dao.rmUserByUser(userId);
        schoolIds = schoolIds.stream().distinct().filter(id->id!=null).collect(Collectors.toList());
        //添加
        if(schoolIds.size()>0){
            dao.addSchool(schoolIds,userId);
        }

    }
}
